const citiesMap = require('./citiesMap');

function mapcity() {
    const provinces = citiesMap.filter(item => item.id.slice(2) === '0000').map(item => ({
        provinceCode: item.id.slice(0, 2),
        provinceId: item.id,
        provinceName: item.name,
        cities: [],
    }));
    const cities = citiesMap.filter(item => item.id.slice(2, 4) !== '00' && item.id.slice(4, 6) === '00').map(
        item => ({
            provinceCode: item.id.slice(0, 2),
            cityCode: item.id.slice(2, 4),
            cityId: item.id,
            cityName: item.name,
            areas: [],
        })
    )
    for(const item of citiesMap) {
        const provinceCode = item.id.slice(0, 2);
        const cityCode = item.id.slice(2, 4);
        const areaCode = item.id.slice(4, 6);
        for(const o of cities) {
            if(provinceCode === o.provinceCode && cityCode === o.cityCode && areaCode !== '00') {
                o.areas.push({
                    id: item.id,
                    name: item.name,
                })
            }
        }
    }
    for(const item of cities) {
        for(const o of provinces) {
            if(item.provinceCode === o.provinceCode) {
                o.cities.push({
                    id: item.cityId,
                    name: item.cityName,
                    areas: item.areas,
                })
            }
        }
    }
    return provinces.map(o => ({
        id: o.provinceId,
        name: o.provinceName,
        cities: o.cities,
    }))
}


module.exports = mapcity();
